package com.zw.pdm.module.est.service;

import com.zw.pdm.entity.basic.Equ;
import com.zw.pdm.entity.basic.EquType;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.est.CardDetail;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.basic.repository.OrgRepository;
import com.zw.pdm.module.craft.repository.CraftMakeDetailRepository;
import com.zw.pdm.module.designtask.repository.DsnProStructRepository;
import com.zw.pdm.module.est.mapper.CardMapper;
import com.zw.pdm.module.est.repository.CardDetailRepository;
import com.zw.pdm.module.est.repository.CardRepository;
import com.zw.pdm.module.report.mapper.SheetMapper;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.CommonUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class CreateCardService {

    @Autowired
    private CardRepository cardRepository;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardDetailRepository cardDetailRepository;
    @Autowired
    private CraftMakeDetailRepository craftMakeDetailRepository;
    @Autowired
    private SheetMapper sheetMapper;
    @Autowired
    private OrgRepository orgRepository;
    @Autowired
    private LogService logService;
    @Autowired
    private DsnProStructRepository dsnProStructRepository;

    private String msg = "";
    @Value("${user.upload.path}")
    private String uploadPath;


    /** 保存工艺卡 */
    @Transactional(rollbackFor = Exception.class)
    public int save(Card card, String[] route, Integer[] orderNo, String[] process, String[] equName, String[] content) {
        DsnProStruct dsnProStruct = dsnProStructRepository.getOne(card.getPartId());
        card.setPart(dsnProStruct.getProPart());
        Card c = cardMapper.findByDrawNo(card.getProCode(), card.getPartId(), card.getDrawNo());
        if (c != null) {
            return 0;
        }

        User u = ShiroUtils.getUser();
        card.setCreateCodeOne(u.getAccount());
        card.setCreateNameOne(u.getName());
        card.setDeptId(u.getDept());
        card.setDeptName(u.getDeptName());
        card.setBranchId(u.getDeptId());
        card.setBranchName(u.getBranchName());
        card.setCreateTime(new Date());
        card.setIsDel(0);
        card.setIsCheck(0);
        card.setIsQuota(0);
        card.setStatus(1);
        card.setMayChange(0);
        cardRepository.save(card);

        // 设置制作件清单明细为已处理
        /*CraMakeDetail craMakeDetail = craftMakeDetailRepository.getOne(card.getCraMakeDetailId());
        craMakeDetail.setChangeStatus(1);
        craftMakeDetailRepository.save(craMakeDetail);*/

        for (int i = 0; i < route.length; i++) {
            CardDetail detail = new CardDetail();
            detail.setCardId(card.getId());
            detail.setRoute(route[i]);
            detail.setOrderNo(orderNo[i]);
            detail.setProcess(process[i]);
            if (equName.length > 0) {
                detail.setEquName(equName[i]);
            }
            detail.setCraftContent(CommonUtils.handlerEnterAndSpace(content[i]));
            detail.setCreateTime(new Date());
            detail.setIsDel(0);
            detail.setMayChange(0);
            detail.setOrderNum(i);
            detail = cardDetailRepository.save(detail);
        }
        logService.saveLog("创建工艺卡", "工艺卡ID：" + card.getId());
        return 1;
    }

    /**
     * 根据工序查询设备
     * @param name
     * @return
     */
    public List<Equ> findBufByName(String name) {
        return  cardMapper.findBufByName(name);
    }
}
